﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;

namespace Base.ModuleBase
{
    public class DialogViewModelBase : ViewModelBase, IDialogAware
    {
        #region 成员
        /// <summary>
        /// 关闭委托
        /// </summary>
        public event Action<IDialogResult> RequestClose;

        #endregion

        #region 属性

        private string _title;
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public bool IsOpened { get; private set; }
        #endregion

        #region 方法

        #region 命令
        private DelegateCommand<string> _closeCommand;
        /// <summary>
        /// 关闭命令
        /// </summary>
        public DelegateCommand<string> CloseCommand => _closeCommand ?? (_closeCommand = new DelegateCommand<string>(OnCloseCommand)); 
        #endregion

        /// <summary>
        /// 窗口打开事件
        /// </summary>
        /// <param name="parameters"></param>
        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            IsOpened = true;
        } 

        public virtual void CloseDialog(ButtonResult buttonResult = ButtonResult.None)
        {
            RequestClose?.Invoke(new DialogResult(buttonResult));
        }

        /// <summary>
        /// 是否可以关闭
        /// </summary>
        /// <returns></returns>
        public virtual bool CanCloseDialog()
        {
            return true;
        }

        /// <summary>
        /// 窗口关闭事件
        /// </summary>
        public virtual void OnDialogClosed()
        {
            IsOpened = false;
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="parameter"></param>
        private void OnCloseCommand(string parameter)
        {
            ButtonResult result = ButtonResult.None;

            switch (parameter)
            {
                case "1":
                    result = ButtonResult.OK;
                    break;
                case "0":
                    result = ButtonResult.Cancel;
                    break;
            }

            CloseDialog(result);
        }

        #endregion
    }
}
